/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import Controller.AccountManager;
import Controller.LogManager;
import Model.login;
import java.io.IOException;
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author pauld
 */
@WebServlet(name = "createManager", urlPatterns = {"/createManager"})
public class createManager extends HttpServlet {
    private static String username = null,
            password1 = null,
            password2 = null,
            type = null;
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        
        int result = AccountManager.addManager(username, password1, type);
            
            //check if the user was successfully added
            if( result == 1 ){//success
                LogManager.logEvent("CreateManager." + Thread.currentThread().getStackTrace()[1].getMethodName(), Level.INFO, "Create Manager Successful: " + username, request.getRemoteAddr(), String.valueOf(request.getSession().getAttribute("name")));
                response.sendRedirect("admin/adminPanel.jsp?status=1");
            }
            else if( result == 0 ){//username taken
                LogManager.logEvent("CreateManager." + Thread.currentThread().getStackTrace()[1].getMethodName(), Level.WARNING, "Create Manager Unsuccessful - Username Taken: " + username, request.getRemoteAddr(), String.valueOf(request.getSession().getAttribute("name")));
                response.sendRedirect("admin/adminPanel.jsp?status=-1");
            }
            else if( result == -1 ){//db error
                LogManager.logEvent("CreateManager." + Thread.currentThread().getStackTrace()[1].getMethodName(), Level.SEVERE, "Create Manager Unsuccessful - Database Error", request.getRemoteAddr(), String.valueOf(request.getSession().getAttribute("name")));
                response.sendRedirect("admin/adminPanel.jsp?status=-2");
            }
    }
    
    //method for checking the user inputs
    //returns boolean 
    //true if user inputs are valid and fals otherwise
    private static boolean checkInput(HttpServletRequest request){
        //get the form values and store them on the variables
        username = request.getParameter("manager-username");
        password1 = request.getParameter("manager-pw1");
        password2 = request.getParameter("manager-pw2");
        type = request.getParameter("manager-type");
        
        //validate user inputs through regex
        String regexAlphaNum = "^[A-Za-z0-9]+$"; //whitelist
        Pattern pAlphaNum = Pattern.compile(regexAlphaNum, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
        Matcher mUsername = pAlphaNum.matcher(username),
                mPW1 = pAlphaNum.matcher(username),
                mPW2 = pAlphaNum.matcher(username);
        boolean resultUsername = mUsername.find(),
                resultPW1 = mPW1.find(),
                resultPW2 = mPW2.find();

        //check the length of the user inputs
        if( (username.length() >= 4 && username.length() <= 25) && 
            (password1.length() >= 8 && password1.length() <= 25) &&
            (password2.length() >= 8 && password2.length() <= 25) &&
            password1.equals(password2) &&
                resultUsername && 
                resultPW1 && 
                resultPW2 )
            return true;
        else
        {
            LogManager.logEvent("CreateManager." + Thread.currentThread().getStackTrace()[1].getMethodName(), Level.WARNING, "Create Manager Unsuccessful - Invalid Input", request.getRemoteAddr(), String.valueOf(request.getSession().getAttribute("name")));
            return false;
        }
    }
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        if( checkInput(request) )
            processRequest(request, response);
        else
            response.sendRedirect("admin/adminPanel.jsp?status=0");//fill up form correctly
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        if( checkInput(request) )
            processRequest(request, response);
        else
            response.sendRedirect("admin/adminPanel.jsp?status=0");//fill up form correctly
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }
    
}
